import sys,pygame
from bullet import Bullet
from alien import Alien
from time import sleep


def check_keydown_events(event,
                         ship,
                         screen,
                         ai_settings,
                         bullets,
                         stats,
                         aliens):
        if event.key == pygame.K_RIGHT:
            ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            ship.moving_left = True
        elif event.key == pygame.K_SPACE:
            fire_bullet(ai_settings,
                        screen,
                        ship,
                        bullets)
        elif event.key == pygame.K_r:
            start_game(ai_settings,
                       screen,
                       stats,
                       aliens,
                       bullets,
                       ship)

def check_keyup_event(event,
                      ship,
                      screen,
                      ai_settings,
                      bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False


def check_events(ai_settings,
                 screen,
                 stats,
                 sb,
                 play_button,
                 ship,
                 aliens,
                 bullets,):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,
                                 ship,
                                 screen,
                                 ai_settings,
                                 bullets,
                                 stats,
                                 aliens)
        elif event.type == pygame.KEYUP:
            check_keyup_event(event,
                              ship,
                              screen,
                              ai_settings,
                              bullets)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y = pygame.mouse.get_pos()
            check_play_button(ai_settings,
                        screen,
                        stats,
                        sb,
                        play_button,
                        ship,
                        aliens,
                        bullets,
                        mouse_x,
                        mouse_y)


def check_play_button(ai_settings,
                      screen,
                      stats,
                      sb,
                      play_button,
                      ship,
                      aliens,
                      bullets,
                      mouse_x,
                      mouse_y):
    button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)
    if button_clicked and not stats.game_active:

        stats.reset_stats()
        stats.game_active = True

        sb.prep_high_score()
        sb.prep_score()
        sb.prep_level()
        sb.prep_ships()

        aliens.empty()
        bullets.empty()
        create_fleet(ai_settings, screen, ship, aliens)
        ship.center_ship()

        pygame.mouse.set_visible(False)


# play_button.rect.collidepoint(mouse_x, mouse_y):


def start_game(ai_settings,
               screen,
               stats,
               aliens,
               bullets,
               ship):
    stats.reset_stats()
    stats.game_active = True
    aliens.empty()
    bullets.empty()
    create_fleet(ai_settings, screen, ship, aliens)
    ship.center_ship()
    ai_settings.initialize_dynamic()


def update_screen(ai_settings,
                  screen,
                  stats,
                  sb,
                  ship,
                  aliens,
                  bullets,
                  play_button):
    screen.fill(ai_settings.bg_color)
    # The bullets
    for bullet in bullets:
        bullet.draw_bullet()
    ship.blitme()
    aliens.draw(screen)
    pygame.display.update()

    # Your score
    sb.show_score()
    # ##########################
    if not stats.game_active:
        play_button.draw_button()
    # Make the screen visible
    pygame.display.flip()

def update_bullets(ai_settings,
                   screen,
                   stats,
                   sb,
                   ship,
                   aliens,
                   bullets):
    bullets.update()

    for bullet in bullets.copy():
        if bullet.rect.y <= 50:
            bullets.remove(bullet)
    check_bullets_collision(ai_settings,
                            screen,
                            stats,
                            sb,
                            ship,
                            aliens,
                            bullets)


def check_bullets_collision(ai_settings,
                            screen,
                            stats,
                            sb,
                            ship,
                            aliens,
                            bullets):
    collisions = pygame.sprite.groupcollide(bullets,
                                            aliens,
                                            True,
                                            True)
    if collisions:
        for aliens__ in collisions.values():
            stats.score += ai_settings.alien_points*len(aliens__)
            sb.prep_score()
        check_high_score(stats,sb)

    if len(aliens) <= 0:
        bullets.empty()
        ai_settings.increase_speed()
        # Update level string
        stats.level += 1
        sb.prep_level()
        # Renew a fleet of alien
        create_fleet(ai_settings,
                     screen,
                     ship,
                     aliens)


def fire_bullet(ai_settings,
                screen,
                ship,
                bullets):
    if len(bullets) < ai_settings.bullet_allowed:
        new_bullet = Bullet(ai_settings=ai_settings,
                            ship=ship,
                            screen=screen)
        bullets.add(new_bullet)
def get_number_alien_x(ai_settings,
                       alien_width):
    available_x = ai_settings.screen_width-2*alien_width
    number_alien_x = int(available_x / (2 * alien_width))
    return number_alien_x
def get_number_rows(ai_settings,
                    ship_height,
                    alien_height):
    available_y = ai_settings.screen_height - (3*alien_height) -ship_height
    return int(available_y/(2*alien_height))


def create_alien(ai_settings,
                 screen,
                 aliens,
                 alien_number,
                 row_number,
                 my_color):
    alien = Alien(ai_settings,screen,my_color=my_color)
    # This is x-axis
    alien_width = alien.rect.width
    alien.x = alien_width+2*alien_width*alien_number
    alien.rect.x = alien.x
    # This is y-axis
    alien.rect.y = alien.rect.height+2*alien.rect.height*row_number
    # That's all.
    aliens.add(alien)


def create_fleet(ai_settings,
                 screen,
                 ship,
                 aliens):
    alien = Alien(ai_settings,screen,my_color=2)
    for row_number in range(get_number_rows(ai_settings,
                                            ship.rect.height,
                                            alien.rect.height)):
        for alien_number in range(get_number_alien_x(ai_settings,
                                                     alien.rect.width)):
            create_alien(ai_settings,
                         screen,
                         aliens,
                         alien_number,
                         row_number,
                         alien_number*row_number)


def check_fleet_edges(ai_settings,
                      aliens):
    for alien in aliens.sprites():
        if alien.check_edge():
            change_fleet_direction(ai_settings,aliens)
            break
def change_fleet_direction(ai_settings,
                           aliens):
    for alien in aliens.sprites():
        alien.rect.y += ai_settings.fleet_drop_speed
    ai_settings.direction *= -1
def ship_hit(ai_settings,
                 stats,
                 screen,
                 sb,
                 ship,
                 aliens,
                 bullets):
    if stats.ships_left > 0:
        # Update your boats infos
        stats.ships_left -= 1
        sb.prep_ships()

        # Reset the game parameters
        aliens.empty()
        bullets.empty()
        create_fleet(ai_settings,
                     screen,
                     ship,
                     aliens)
        ship.center_ship()
        sleep(0.8)
    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)


def check_aliens_bottom(ai_settings,
                        stats,
                        screen,
                        sb,
                        ship,
                        aliens,
                        bullets):
    screen_rect = screen.get_rect()
    for alien in aliens.sprites():
        if alien.rect.bottom >= screen_rect.bottom-1:
            ship_hit(ai_settings,
                      stats,
                      screen,
                      sb,
                      ship,
                      aliens,
                      bullets)
            break


def update_aliens(ai_settings,
                  stats,
                  screen,
                  sb,
                  ship,
                  aliens,
                  bullets):
    check_fleet_edges(ai_settings,aliens)
    aliens.update()
    if pygame.sprite.spritecollideany(ship,aliens):
        ship_hit(ai_settings,
                 stats,
                 screen,
                 sb,
                 ship,
                 aliens,
                 bullets)
    check_aliens_bottom(ai_settings,
                        stats,
                        screen,
                        sb,
                        ship,
                        aliens,
                        bullets)


def check_high_score(stats,
                     sb):
    if stats.score > stats.high_score:
        stats.high_score = stats.score
        sb.prep_high_score()










































